package net.dgg.zz.credential.capitalPoolCount.service;

import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.base.service.BaseService;
import net.dgg.zz.common.systemRecord.entity.SystemRecord;
import net.dgg.zz.common.systemRecord.service.SystemRecordService;
import net.dgg.zz.common.utils.StringUtils;
import net.dgg.zz.common.utils.ValidateUtils;
import net.dgg.zz.credential.baseCredential.entity.BaseCredential;
import net.dgg.zz.credential.capitalPoolCount.dao.CapitalPoolCountDao;
import net.dgg.zz.credential.capitalPoolCount.entity.CapitalPoolCount;
import net.dgg.zz.credential.excption.CertificateExcption;
import net.dgg.zz.credential.generalCredential.dao.GeneralCertificateDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 资金池记录Service
 *
 * @author wmj
 * @date 2018/10/11 0011
 */
@Service
public class CapitalPoolCountService extends BaseService {
    @Resource
    private SessionManager sessionManager;
    @Resource
    private SystemRecordService systemRecordService;
    @Resource
    private CapitalPoolCountDao capitalPoolCountDao;
    @Resource
    private GeneralCertificateDao generalCertificateDao;

    /**
     * 添加资金池记录
     *
     * @param baseCredential  证书基本信息实体 至少包含证书id 证书编号 证书所属事业部id  证书所属事业部名称 证书类型（普通证书or资质）
     * @param user            操作人
     * @param money           金额
     * @param operateType     类型：0-费用申请通过 1-使用
     * @param businessOrderId 销售子订单id(订单系统id)
     */
    @Transactional
    public void addCapitalPoolCountRecord(BaseCredential baseCredential, UserEntity user, BigDecimal money, Integer operateType, Long businessOrderId) {

        if (StringUtils.isNotEmpty(baseCredential)) {
            ValidateUtils.isTrue(StringUtils.isNotEmpty(baseCredential.getId()), CertificateExcption.class, "证书Id不能为空");
            ValidateUtils.isTrue(StringUtils.isNotEmpty(baseCredential.getCertificateNo()), CertificateExcption.class, "证书编号不能为空");
            ValidateUtils.isTrue(StringUtils.isNotEmpty(baseCredential.getSignOrgId()), CertificateExcption.class, "证书所属事业部id不能为空");
            ValidateUtils.isTrue(StringUtils.isNotEmpty(baseCredential.getSignOrgName()), CertificateExcption.class, "证书所属事业部名称不能为空");
            ValidateUtils.isTrue(StringUtils.isNotEmpty(baseCredential.getGeneralType()), CertificateExcption.class, "证书类型不能为空");
        }
        ValidateUtils.isTrue(StringUtils.isNotEmpty(money), CertificateExcption.class, "金额不能为空");
        ValidateUtils.isTrue(StringUtils.isNotEmpty(operateType), CertificateExcption.class, "操作类型不能为空");
        // 如果操作人为空则默认系统当前登录人
        if (StringUtils.isEmpty(user)) {
            user = sessionManager.getCurrentUser();
        }
        // 添加资金池操作记录
        CapitalPoolCount capitalPoolCount = new CapitalPoolCount();
        if (operateType == 0) { //费用申请通过
            capitalPoolCount.setPayMoney(money);
        } else {
            capitalPoolCount.setHangUpMoney(money);
        }

        capitalPoolCount.setId(DggKeyWorker.nextId());
        // 如果是证书则填写相关字段
        if (StringUtils.isNotEmpty(baseCredential)) {
            capitalPoolCount.setGeneralType(baseCredential.getGeneralType());
            capitalPoolCount.setBaseCertificateId(baseCredential.getId());
            capitalPoolCount.setCertificateNo(baseCredential.getCertificateNo());
            capitalPoolCount.setSignOrgId(baseCredential.getSignOrgId());
            capitalPoolCount.setSignOrgName(baseCredential.getSignOrgName());
            // 如果销售子订单id不为空，则将该销售子订单的费用申请记录查询出来并且绑定上证书信息
            if (StringUtils.isNotEmpty(businessOrderId)) {
                // 如果传回的销售子订单id与证书创建时绑定的销售子订单id相等
                Map businessOrder = generalCertificateDao.findOrderBusinessInfoByCertificate(baseCredential.getId());
                if (StringUtils.isNotEmpty(businessOrder) && businessOrder.get("orderBusinessId").toString().equals(businessOrderId.toString())) {

                    // 将该销售子订单之前的费用申请全部绑定到现在的证书上
                    List<CapitalPoolCount> list = capitalPoolCountDao.findCountByBusinessOrderId(businessOrderId, 1);
                    if (StringUtils.isNotEmpty(list)) {
                        for (CapitalPoolCount poolCount : list) {
                            poolCount.setGeneralType(baseCredential.getGeneralType());
                            poolCount.setBaseCertificateId(baseCredential.getId());
                            poolCount.setCertificateNo(baseCredential.getCertificateNo());
                            poolCount.setSignOrgId(baseCredential.getSignOrgId());
                            poolCount.setSignOrgName(baseCredential.getSignOrgName());
                            poolCount.setUpdaterUser(user);
                            poolCount.setIsCertificate(0);
                            capitalPoolCountDao.updateByPrimaryKeySelective(poolCount);
                        }
                    }
                }
            }
            capitalPoolCount.setIsCertificate(0);
        } else { //费用申请
            capitalPoolCount.setIsCertificate(1);
        }
        capitalPoolCount.setBusinessOrderId(businessOrderId);
        capitalPoolCount.setCreateUser(user);
        capitalPoolCountDao.insert(capitalPoolCount);

        // 保存操作日志
        SystemRecord systemRecords = new SystemRecord();
        systemRecords.setSource("1");
        systemRecords.setOperateType("1");
        systemRecords.setWorkTableName("zz_general_certificate");
        systemRecords.setWorkId(capitalPoolCount.getId());
        if (StringUtils.isNotEmpty(baseCredential)) {
            systemRecords.setWorkNo(baseCredential.getCertificateNo());
            systemRecords.setCertificateName(baseCredential.getCertificateName());
            systemRecords.setCertificateCode(baseCredential.getCertificateNo());
            systemRecords.setSignOrgId(baseCredential.getSignOrgId());
            systemRecords.setSignOrgName(baseCredential.getSignOrgName());
            systemRecords.setCertificateType(baseCredential.getCertificateType());
        }
        systemRecords.setRemarks("证书中心：添加资金池记录");
        systemRecordService.apiSave(systemRecords, user);

    }
}
